/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package b_and_b_properties;

import b_and_b_properties.data.DAO;
import java.awt.BorderLayout;

import java.awt.GridLayout;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.util.List;
import javax.swing.*;
import java.sql.*;

/**
 *
 * @author Mark, LeQuan, Rob
 */
public class SearchInterface extends JFrame {

    private DAO daoObject;
//create the combo box for price range
    private String[] ranges = {" ", "0-19,000", "20,000-39,000", "40,000 - 50,000", "50,000 +"};
    private JTextField streetNameTextField = new JTextField(20);
    private JTextField cityTextField = new JTextField(10);
    
    public SearchInterface(DAO daoObject) {
        super("Search");
        this.daoObject = daoObject;
        setLayout(new BorderLayout(5,5));
        setSize(280, 135);
        createSearchPanel();
         // connect to the driver
        try {
            Class.forName("com.mysql.jdbc.Driver");
            String connectionUrl = "jdbc:mysql://localhost/mysql?" +
                                   "user=root&password=Capstone2011";
            Connection con = DriverManager.getConnection(connectionUrl);
            Statement stmt = null;
            ResultSet rs = null;
            //SQL query command
            String SQL = "SELECT * FROM SAPCAPSTONE";
            stmt = con.createStatement();
            rs = stmt.executeQuery(SQL);
              while (rs.next()) {
                System.out.println(rs.getString("StreetName") + " : " + rs.getString("City"));
            }     
            
        } catch (SQLException e) {
            System.out.println("SQL Exception: "+ e.toString());
        } catch (ClassNotFoundException cE) {
            System.out.println("Class Not Found Exception: "+ cE.toString());
        }  
         
    }
    public void createSearchPanel(){
        JPanel searchPanel = new JPanel(new GridLayout(3, 2, 5, 5));
        searchPanel.add(new JLabel("Street Name"));
        searchPanel.add(streetNameTextField);
        searchPanel.add(new JLabel("City/Municipality"));
        searchPanel.add(cityTextField);
        searchPanel.add(new JLabel("Price:"));
        searchPanel.add(new JComboBox(ranges));
        add(searchPanel,BorderLayout.NORTH);
        JButton searchButton = new JButton("Search");
        add(searchButton,BorderLayout.SOUTH);
        searchButton.addActionListener(new ActionListener(){

            public void actionPerformed(ActionEvent e) {
                List returned = daoObject.getEntityManager().createNamedQuery("Property.findByStreetName").setParameter("streetName",streetNameTextField.getText()).getResultList();
                JOptionPane.showMessageDialog(null, returned.get(0));
            }
        });
    }
}
